//
//  UITextField+CustonFont.m
//  FRQBluetoothKit_Example
//
//  Created by admin on 2023/12/13.
//  Copyright © 2023 xuchunhai. All rights reserved.
//

#import "UITextField+CustonFont.h"
#import <objc/runtime.h>
@implementation UITextField (CustonFont)
+(void)load{
    
    [super load];
    //只执行一次这个方法
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        // When swizzling a class method, use the following:
        // Class class = object_getClass((id)self);
//        - (nullable instancetype)initWithCoder:(NSCoder *)coder
        //替换三个方法
        SEL originalSelector = @selector(init);
        SEL originalSelector2 = @selector(initWithFrame:);
        SEL originalSelector3 = @selector(awakeFromNib);
        SEL originalSelector4 = @selector(initWithCoder:);
        
        SEL swizzledSelector = @selector(CFInit);
        SEL swizzledSelector2 = @selector(CFInitWithFrame:);
        SEL swizzledSelector3 = @selector(CFAwakeFromNib);
        SEL swizzledSelector4 = @selector(CFInitWithCoder:);
        
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
        Method originalMethod3 = class_getInstanceMethod(class, originalSelector3);
        Method originalMethod4 = class_getInstanceMethod(class, originalSelector4);
        
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);
        Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3);
        Method swizzledMethod4 = class_getInstanceMethod(class, swizzledSelector4);
        
        BOOL didAddMethod =
        class_addMethod(class,
                        originalSelector,
                        method_getImplementation(swizzledMethod),
                        method_getTypeEncoding(swizzledMethod));
        BOOL didAddMethod2 =
        class_addMethod(class,
                        originalSelector2,
                        method_getImplementation(swizzledMethod2),
                        method_getTypeEncoding(swizzledMethod2));
        BOOL didAddMethod3 =
        class_addMethod(class,
                        originalSelector3,
                        method_getImplementation(swizzledMethod3),
                        method_getTypeEncoding(swizzledMethod3));
        
        BOOL didAddMethod4 =
        class_addMethod(class,
                        originalSelector4,
                        method_getImplementation(swizzledMethod4),
                        method_getTypeEncoding(swizzledMethod4));
        
        if (didAddMethod) {
            class_replaceMethod(class,
                                swizzledSelector,
                                method_getImplementation(originalMethod),
                                method_getTypeEncoding(originalMethod));
            
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
        if (didAddMethod2) {
            class_replaceMethod(class,
                                swizzledSelector2,
                                method_getImplementation(originalMethod2),
                                method_getTypeEncoding(originalMethod2));
        }else {
            method_exchangeImplementations(originalMethod2, swizzledMethod2);
        }
        if (didAddMethod3) {
            class_replaceMethod(class,
                                swizzledSelector3,
                                method_getImplementation(originalMethod3),
                                method_getTypeEncoding(originalMethod3));
        }else {
            method_exchangeImplementations(originalMethod3, swizzledMethod3);
        }
        if (didAddMethod4) {
            class_replaceMethod(class,
                                swizzledSelector4,
                                method_getImplementation(originalMethod4),
                                method_getTypeEncoding(originalMethod4));
        }else {
            method_exchangeImplementations(originalMethod4, swizzledMethod4);
        }
    });
    
}
/**
 *在这些方法中将你的字体名字换进去    FZZJ-ZSCWBJW
 */
- (instancetype)CFInit
{
    id __self = [self CFInit];
    [self setFontFamily:@"Montserrat-SemiBold" forView:self andSubViews:YES];
    return __self;
}

-(instancetype)CFInitWithFrame:(CGRect)rect{
    id __self = [self CFInitWithFrame:rect];
    [self setFontFamily:@"Montserrat-SemiBold" forView:self andSubViews:YES];
    return __self;
}
-(instancetype)CFInitWithCoder:(CGRect)rect{
    id __self = [self CFInitWithCoder:rect];
    [self setFontFamily:@"Montserrat-SemiBold" forView:self andSubViews:YES];
    return __self;
}
-(void)CFAwakeFromNib{
    [self CFAwakeFromNib];
    
    UIFont * font = [UIFont fontWithName:@"Montserrat-Bold" size:self.font.pointSize];
    if (font) {
        self.font=font;
    }
}
- (void)setFontFamily:(NSString*)fontFamily forView:(UIView*)view andSubViews:(BOOL)isSubViews
{
    if ([view isKindOfClass:[UILabel class]])
    {
        UILabel *lbl = (UILabel *)view;
        [lbl setFont:[UIFont fontWithName:fontFamily size:[[lbl font] pointSize]]];
    }
    if([view isKindOfClass:[UIButton class]])
    {
        UIButton *btn = (UIButton *)view;
        [btn.titleLabel setFont:[UIFont fontWithName:fontFamily size:[[btn.titleLabel font] pointSize]]];
    }
    if([view isKindOfClass:[UITextField class]])
    {
        UITextField *txt = (UITextField *)view;
        [txt setFont:[UIFont fontWithName:fontFamily size:[[txt font] pointSize]]];
    }
    if (isSubViews)
    {
        for (UIView *sview in view.subviews)
        {
            [self setFontFamily:fontFamily forView:sview andSubViews:YES];
        }
    }
}
@end
